package com.wss.juc;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;

public class MeetingExample {
    public static void main(String[] args) {
        final int EMPLOYEE_COUNT = 5;

        CyclicBarrier barrier = new CyclicBarrier(EMPLOYEE_COUNT, () -> {
            System.out.println("所有员工已到齐，会议开始！");
        });


        for (int i = 1; i <= EMPLOYEE_COUNT; i++) {
            final int employeeId = i;
            new Thread(() -> {
                try {

                    long arrivalTime = (long) (Math.random() * 5000);
                    Thread.sleep(arrivalTime);
                    System.out.println("员工" + employeeId + "已到达会议室");

                    barrier.await();

                    System.out.println("员工" + employeeId + "准备开始会议...");
                } catch (InterruptedException | BrokenBarrierException e) {
                    Thread.currentThread().interrupt();
                    System.err.println("员工" + employeeId + "中断：" + e.getMessage());
                }
            }).start();
        }
    }
}